package com.ljw.spring.annotation.test;

import com.ljw.spring.annotation.bean.Book;
import com.ljw.spring.annotation.bean.ColorFactoryBean;
import com.ljw.spring.annotation.bean.User;
import com.ljw.spring.annotation.conf.MyConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @FileName IOCtest
 * @Description TODO springIoc容器测试---基于注解 开发
 * @Author ljw
 * @Date 2021/2/1 3:56
 * @Version 1.0
 */
public class IOCtest {

   @Test
    public void test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User user1 = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        /*Object user1 = context.getBean( "user");
        Object user2 = context.getBean( "user");*/
       /**
        * 1. "=="是判断两个变量或实例是不是指向同一个内存空间。
        * 2. "equals"是判断两个变量或实例所指向的内存空间的值是不是相同。
        * 如下,当IOC容器中的User对象多实例时【@Scope(value = "prototype")】，结果：
        *
        */
       System.out.println(user1==user2);//false
       System.out.println(user1.equals(user2));//true
       System.out.println(user1);//User(name=ljw)
       System.out.println(user2);//User(name=ljw)

        //获取在IOC容器中注册的所有实例对象
       String[] beanNames = context.getBeanDefinitionNames();
       for (String beanName : beanNames) {
           System.out.println("ioc容器中的bean："+beanName);
       }
   }

    @Test
    public void test02() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        //获取在IOC容器中注册的所有Book.class类型的实例对象bean的id名
        String[] beanNames = context.getBeanNamesForType(Book.class);
        for (String beanName : beanNames) {
            System.out.println("在IOC容器中注册的Book.class类型的实例对象bean："+beanName);
        }

        //获取在IOC容器中注册的所有Book.class类型的bean实例对象
        Map<String, Book> beans = context.getBeansOfType(Book.class);
        System.out.println(beans);

        //获取环境变量
        ConfigurableEnvironment environment = context.getEnvironment();
        //操作系统名
        String property = environment.getProperty("os.name");
        System.out.println(property);
    }

    @Test
    public void testImport(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        //获取在IOC容器中注册的所有实例对象
        String[] beanNames = context.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println("ioc容器中的bean："+beanName);
        }
    }

    @Test
    public void testFactoryBean(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        //获取在IOC容器中注册的所有实例对象
        String[] beanNames = context.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println("ioc容器中的bean："+beanName);
            if (beanName.equals("colorFactoryBean")){
                Object colorFactoryBean1 = context.getBean("colorFactoryBean");
                Object colorFactoryBean2 = context.getBean("colorFactoryBean");

                //1. 是ColorFactoryBean.getObject()方法返回的类的类型，不是ColorFactoryBean的类型
                System.out.println("IOC容器中bean名【colorFactoryBean】的类型："+colorFactoryBean1.getClass());//colorFactoryBean的类型：class com.ljw.spring.annotation.bean.Color

                //2. 要想获取ColorFactoryBean自身的类型，则需要在IOC容器中bean名“colorFactoryBean”前添加前缀“&”
                Object colorFactoryBean3 = context.getBean("&colorFactoryBean");
                System.out.println("ColorFactoryBean类自身的类型："+colorFactoryBean3.getClass());

                //ColorFactoryBean.isSingleton()方法限制了是单实例还是多实例
                System.out.println(colorFactoryBean1==colorFactoryBean2);


            }
        }

    }

}
